package org.fuxia.im.common;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;


public class MyMessageDialog extends JWindow
{
	private static final long	serialVersionUID	= -8841667569141497455L;

	private Font font;
	private final JPanel	contentPane;

	private String 				titleStr = "新消息到达提示";
	private Timer				t;
	private JPanel panel;
	private boolean hasClicked = false;
	private JEditorPane messagePane;
	private Map<Object,Object>  context ;
	private MessageDialogClickHandler clickHandler ;
	private boolean isClicked ;
	private Long delay = 5000L ;

	public MyMessageDialog(String titleStr, String tips)
	{
		context = new HashMap<Object, Object>();
		font = new Font("微软雅黑", Font.PLAIN, 12);
		t = new Timer();

		this.titleStr = titleStr;
		setSize(204, 139) ;
		contentPane = new JPanel();
		contentPane.setBorder(UIManager.getBorder("FormattedTextField.border"));
		setContentPane(contentPane);

		panel = new JPanel();
		messagePane = new JEditorPane();
		messagePane.setBackground(UIManager.getColor("Button.background"));
		messagePane.setForeground(new Color(0, 0, 0));
		messagePane.setEditable(false);
		messagePane.setText(tips);
		messagePane.setFont(font);
		GroupLayout layout = new GroupLayout(contentPane);
		layout.setHorizontalGroup(layout.createParallelGroup(Alignment.LEADING).addGroup(layout
				.createSequentialGroup()
				.addGroup(layout.createParallelGroup(Alignment.TRAILING, false)
						.addComponent(messagePane, Alignment.LEADING)
						.addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE))
						.addContainerGap(3, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(Alignment.LEADING).addGroup(layout
				.createSequentialGroup()
				.addComponent(panel, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(ComponentPlacement.RELATED)
				.addComponent(messagePane, GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)));
		panel.setLayout(null);
		contentPane.setLayout(layout);

		JButton iconButton = new JButton("");
		iconButton.setBounds(0, 0, 16, 16);
		iconButton.setEnabled(false);
		panel.add(iconButton);

		JButton closeIcon = new JButton("");
		closeIcon.setBounds(180, 0, 16, 16);
		closeIcon.setIcon(new ImageIcon(Thread.currentThread().getContextClassLoader()
				.getResource("images/focusclose.jpg")));
		panel.add(closeIcon);
		closeIcon.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e)
			{
				t.cancel();
				setVisible(false);
				dispose();
			}
		});

		JLabel title = new JLabel(this.titleStr);
		title.setFont(font);
		title.setBounds(18, 1, 92, 15);
		this.addMouseClickListener(new MyMouseListener());
		panel.add(title);
		setAlwaysOnTop(true);

	}

	public void showDialog()
	{
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension dm = tk.getScreenSize();
		this.setLocation(dm.width - this.getWidth(), dm.height - this.getHeight());
		t.schedule(new TimerTask() {
			public void run()
			{
				t.cancel();
				setVisible(false);
				dispose();
			}
		}, delay);
		this.setVisible(true);

	}


	public void addMouseClickListener(MouseListener l) {
		this.contentPane.addMouseListener(l);
		this.panel.addMouseListener(l);
		this.messagePane.addMouseListener(l);
	}


	class MyMouseListener extends MouseAdapter {

		public void mouseClicked(MouseEvent e) {
			if (!hasClicked && e.getClickCount() == 1) {
				SwingUtilities.invokeLater(new Runnable() {
					public void run() {
						/*if(isGroupChat) {
							GroupChat gc = new GroupChat() ;
							ClientContext.putValue(getFromGroupId(), gc);
							gc.setState(JFrame.NORMAL) ;
							gc.showGroupChatWindow(getFromGroupId());
							gc.insertOtherUserHeader(fromUser);
							gc.insertRecvMessae(message);
							
							
						}else {
							User user = ClientContext.findUserByCode(fromUser);
							Chat chat= ClientContext.getValue(user.getUserCode(),
									Chat.class);
							if (chat == null) {
								chat = new Chat(user);
								ClientContext.putValue(user.getUserCode(), chat);
							}
							chat.setVisible(true);
							chat.insertOtherUserHeader(user.getNickName());
							chat.insertRecvMessae(message);
							chat.toFront();
						}*/
						if(!isClicked&&clickHandler!=null) {
							isClicked = true ;
							clickHandler.doAction();
						}
						
					}
				}) ;
				
			}
		}

	}
	
	public void putData(String key,Object data) {
		context.put(key, data);
	}

	public MessageDialogClickHandler getClickHandler() {
		return clickHandler;
	}

	public void setClickHandler(MessageDialogClickHandler clickHandler) {
		this.clickHandler = clickHandler;
	}
	
	public static interface MessageDialogClickHandler{
		public void doAction();
	}

	public Long getDelay() {
		return delay;
	}

	public void setDelay(Long delay) {
		this.delay = delay;
	}
	
}


